package com.djl.spring.redis.util;

import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.lettuce.core.ReadFrom;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static io.lettuce.core.ReadFrom.MASTER;
import static io.lettuce.core.ReadFrom.REPLICA_PREFERRED;

/**
 * 使用 LettuceConnectionFactory
 */
@Configuration
public class RedisConfiguration {


    /**
     * 主从复制 + sentinel 配置工厂
     */
    @Bean
    public LettuceConnectionFactory lettuceConnectionFactory(){
        //主从复制配置
        LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
                .readFrom(REPLICA_PREFERRED)
                .build();

        // Sentinel 配置
        RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration();
        sentinelConfig.master("mymaster");
        sentinelConfig .sentinel("192.168.133.132", 26379);
        sentinelConfig .sentinel("192.168.133.132", 26380);
        sentinelConfig .sentinel("192.168.133.132", 26381);
        return  new LettuceConnectionFactory(sentinelConfig, clientConfig);
    }

    @Bean
    public RedisTemplate redisTemplate(LettuceConnectionFactory redisConnectionFactory) {

        RedisTemplate redisTemplate = new RedisTemplate();

        Jackson2JsonRedisSerializer jsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        jsonRedisSerializer.setObjectMapper(mapper);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        //设置存储 key 的序列化方式
        redisTemplate.setKeySerializer(stringRedisSerializer);
        //设置存储 value 的序列化方式
        redisTemplate.setValueSerializer(jsonRedisSerializer);

        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

    /**
     * 将消息监听适配器加入到容器
     */
    @Bean
    public MessageListenerAdapter getMessageListenerAdapter(CustomMessageListener customMessageListener) {
        return new MessageListenerAdapter(customMessageListener);
    }

    /**
     * 配置监听容器
     */
    @Bean
    public RedisMessageListenerContainer getRedisMessageListenerContainer(LettuceConnectionFactory redisConnectionFactory,MessageListenerAdapter messageListenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        //设置监听管道 ooxx
        List<ChannelTopic> topics = Arrays.asList(new ChannelTopic("ooxx"));
        container.addMessageListener(messageListenerAdapter, topics);
        return container;
    }

}
